package com.sias.oj.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.gson.Gson;
import com.sias.oj.annotation.AuthCheck;
import com.sias.oj.common.BaseResponse;
import com.sias.oj.common.DeleteRequest;
import com.sias.oj.common.ErrorCode;
import com.sias.oj.common.ResultUtils;
import com.sias.oj.constant.UserConstant;
import com.sias.oj.exception.BusinessException;
import com.sias.oj.exception.ThrowUtils;
import com.sias.oj.model.dto.post.PostAddRequest;
import com.sias.oj.model.dto.post.PostEditRequest;
import com.sias.oj.model.dto.post.PostQueryRequest;
import com.sias.oj.model.dto.post.PostUpdateRequest;
import com.sias.oj.model.entity.Post;
import com.sias.oj.model.entity.User;
import com.sias.oj.model.vo.PostVO;
import com.sias.oj.service.PostService;
import com.sias.oj.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 帖子接口
 *
 * @author <a href="https://github.com/sogra">程序员sogra</a>
 */
@RestController
@RequestMapping("/post")
@Slf4j
public class PostController {

  private final static Gson GSON = new Gson();
  @Resource
  private PostService postService;
  @Resource
  private UserService userService;

  // region 增删改查

  /**
   * 创建
   *
   * @param postAddRequest
   * @param request
   * @return
   */
  @PostMapping("/add")
  public BaseResponse<Long> addPost(@RequestBody PostAddRequest postAddRequest, HttpServletRequest request) {
    if (postAddRequest == null) {
      throw new BusinessException(ErrorCode.PARAMS_ERROR);
    }
    Post post = new Post();
    BeanUtils.copyProperties(postAddRequest, post);
    List<String> tags = postAddRequest.getTags();
    if (tags != null) {
      post.setTags(GSON.toJson(tags));
    }
    postService.validPost(post, true);
    User loginUser = userService.getLoginUser(request);
    post.setUserId(loginUser.getId());
    post.setFavourNum(0);
    post.setThumbNum(0);
    boolean result = postService.save(post);
    ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
    long newPostId = post.getId();
    return ResultUtils.success(newPostId);
  }

  /**
   * 删除
   *
   * @param deleteRequest
   * @param request
   * @return
   */
  @PostMapping("/delete")
  public BaseResponse<Boolean> deletePost(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
    if (deleteRequest == null || deleteRequest.getId() <= 0) {
      throw new BusinessException(ErrorCode.PARAMS_ERROR);
    }
    User user = userService.getLoginUser(request);
    long id = deleteRequest.getId();
    // 判断是否存在
    Post oldPost = postService.getById(id);
    ThrowUtils.throwIf(oldPost == null, ErrorCode.NOT_FOUND_ERROR);
    // 仅本人或管理员可删除
    if (!oldPost.getUserId().equals(user.getId()) && !userService.isAdmin(request)) {
      throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
    }
    boolean b = postService.removeById(id);
    return ResultUtils.success(b);
  }

  /**
   * 更新（仅管理员）
   *
   * @param postUpdateRequest
   * @return
   */
  @PostMapping("/update")
  @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
  public BaseResponse<Boolean> updatePost(@RequestBody PostUpdateRequest postUpdateRequest) {
    if (postUpdateRequest == null || postUpdateRequest.getId() <= 0) {
      throw new BusinessException(ErrorCode.PARAMS_ERROR);
    }
    Post post = new Post();
    BeanUtils.copyProperties(postUpdateRequest, post);
    List<String> tags = postUpdateRequest.getTags();
    if (tags != null) {
      post.setTags(GSON.toJson(tags));
    }
    // 参数校验
    postService.validPost(post, false);
    long id = postUpdateRequest.getId();
    // 判断是否存在
    Post oldPost = postService.getById(id);
    ThrowUtils.throwIf(oldPost == null, ErrorCode.NOT_FOUND_ERROR);
    boolean result = postService.updateById(post);
    return ResultUtils.success(result);
  }

  /**
   * 根据 id 获取
   *
   * @param id
   * @return
   */
  @GetMapping("/get/vo")
  public BaseResponse<PostVO> getPostVOById(long id, HttpServletRequest request) {
    if (id <= 0) {
      throw new BusinessException(ErrorCode.PARAMS_ERROR);
    }
    Post post = postService.getById(id);
    if (post == null) {
      throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
    }
    return ResultUtils.success(postService.getPostVO(post, request));
  }

  /**
   * 分页获取列表（封装类）
   *
   * @param postQueryRequest
   * @param request
   * @return
   */
  @PostMapping("/list/page/vo")
  public BaseResponse<Page<PostVO>> listPostVOByPage(@RequestBody PostQueryRequest postQueryRequest,
                                                     HttpServletRequest request) {
    long current = postQueryRequest.getCurrent();
    long size = postQueryRequest.getPageSize();
    // 限制爬虫
    ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
    Page<Post> postPage = postService.page(new Page<>(current, size),
            postService.getQueryWrapper(postQueryRequest));
    return ResultUtils.success(postService.getPostVOPage(postPage, request));
  }

  /**
   * 分页获取当前用户创建的资源列表
   *
   * @param postQueryRequest
   * @param request
   * @return
   */
  @PostMapping("/my/list/page/vo")
  public BaseResponse<Page<PostVO>> listMyPostVOByPage(@RequestBody PostQueryRequest postQueryRequest,
                                                       HttpServletRequest request) {
    if (postQueryRequest == null) {
      throw new BusinessException(ErrorCode.PARAMS_ERROR);
    }
    User loginUser = userService.getLoginUser(request);
    postQueryRequest.setUserId(loginUser.getId());
    long current = postQueryRequest.getCurrent();
    long size = postQueryRequest.getPageSize();
    // 限制爬虫
    ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
    Page<Post> postPage = postService.page(new Page<>(current, size),
            postService.getQueryWrapper(postQueryRequest));
    return ResultUtils.success(postService.getPostVOPage(postPage, request));
  }

  // endregion

  /**
   * 分页搜索（从 ES 查询，封装类）
   *
   * @param postQueryRequest
   * @param request
   * @return
   */
  @PostMapping("/search/page/vo")
  public BaseResponse<Page<PostVO>> searchPostVOByPage(@RequestBody PostQueryRequest postQueryRequest,
                                                       HttpServletRequest request) {
    long size = postQueryRequest.getPageSize();
    // 限制爬虫
    ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
    Page<Post> postPage = postService.searchFromEs(postQueryRequest);
    return ResultUtils.success(postService.getPostVOPage(postPage, request));
  }

  /**
   * 编辑（用户）
   *
   * @param postEditRequest
   * @param request
   * @return
   */
  @PostMapping("/edit")
  public BaseResponse<Boolean> editPost(@RequestBody PostEditRequest postEditRequest, HttpServletRequest request) {
    if (postEditRequest == null || postEditRequest.getId() <= 0) {
      throw new BusinessException(ErrorCode.PARAMS_ERROR);
    }
    Post post = new Post();
    BeanUtils.copyProperties(postEditRequest, post);
    List<String> tags = postEditRequest.getTags();
    if (tags != null) {
      post.setTags(GSON.toJson(tags));
    }
    // 参数校验
    postService.validPost(post, false);
    User loginUser = userService.getLoginUser(request);
    long id = postEditRequest.getId();
    // 判断是否存在
    Post oldPost = postService.getById(id);
    ThrowUtils.throwIf(oldPost == null, ErrorCode.NOT_FOUND_ERROR);
    // 仅本人或管理员可编辑
    if (!oldPost.getUserId().equals(loginUser.getId()) && !userService.isAdmin(loginUser)) {
      throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
    }
    boolean result = postService.updateById(post);
    return ResultUtils.success(result);
  }

}
